import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QHBoxLayout, QTextEdit
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QFileDialog

class TextEditor(QWidget):
    def __init__(self):
        super().__init__()
        self.current_file = None
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        # 工具栏
        toolbar = QHBoxLayout()
        new_btn = QPushButton('新建')
        open_btn = QPushButton('打开')
        save_btn = QPushButton('保存')
        save_as_btn = QPushButton('另存为')
        
        new_btn.clicked.connect(self.new_file)
        open_btn.clicked.connect(self.open_file)
        save_btn.clicked.connect(self.save_file)
        save_as_btn.clicked.connect(self.save_file_as)
        
        toolbar.addWidget(new_btn)
        toolbar.addWidget(open_btn)
        toolbar.addWidget(save_btn)
        toolbar.addWidget(save_as_btn)
        
        # 文本编辑区
        self.text_edit = QTextEdit()
        
        # 状态栏
        self.status_label = QLabel('就绪')
        
        layout.addLayout(toolbar)
        layout.addWidget(self.text_edit)
        layout.addWidget(self.status_label)
        
        self.setLayout(layout)
        self.setWindowTitle('文本编辑器')
        
    def new_file(self):
        self.text_edit.clear()
        self.current_file = None
        self.status_label.setText('新建文件')
        
    def open_file(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "打开文本文件",
            "",
            "文本文件 (*.txt);;所有文件 (*)"
        )
        
        if file_name:
            try:
                with open(file_name, 'r', encoding='utf-8') as file:
                    content = file.read()
                    self.text_edit.setText(content)
                    self.current_file = file_name
                    self.status_label.setText(f'已打开: {file_name}')
            except Exception as e:
                QMessageBox.critical(self, "错误", f"无法打开文件: {str(e)}")
                
    def save_file(self):
        if self.current_file:
            self._save_to_file(self.current_file)
        else:
            self.save_file_as()
            
    def save_file_as(self):
        file_name, _ = QFileDialog.getSaveFileName(
            self,
            "保存文件",
            self.current_file or "untitled.txt",
            "文本文件 (*.txt);;所有文件 (*)"
        )
        
        if file_name:
            self._save_to_file(file_name)
            self.current_file = file_name
            
    def _save_to_file(self, file_path):
        try:
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(self.text_edit.toPlainText())
            self.status_label.setText(f'已保存: {file_path}')
        except Exception as e:
            QMessageBox.critical(self, "错误", f"无法保存文件: {str(e)}")


            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = TextEditor()
    window.show()
    sys.exit(app.exec_())